package com.wwwarehouse.contract.query_express;

import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.ExpressDetailBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * Created by clannad.shao on 2017/11/6.
 * @author clannad_sk
 * @date 2017/11/6
 */
@Route(path = "/contract/ExpressDetailFragment")
public class ExpressDetailFragment extends CommonBasePagerFragment {

    private static final int QUERY_EXPRESS_DETAIL_CODE = 0x45;
    private String packageUkid;

    @Override
    public void requestDatas() {
        super.requestDatas();
        Bundle bundle = getArguments();
        packageUkid = bundle.getString("packageUkid");
        hideConfirmButton();
        Map<String, Object> map = new HashMap<>();
        map.put("packageUkid", packageUkid);
        loadData(ContractConstant.QUERY_EXPRESS_DETAIL, map, QUERY_EXPRESS_DETAIL_CODE);
    }

    /**
     * 请求成功回调
     * @param data
     * @param what
     */
    @Override
    public void onLoadSuccess(String data, int what) {
        if (what == QUERY_EXPRESS_DETAIL_CODE){
            ExpressDetailBean expressDetail = parseObject(data, ExpressDetailBean.class);
            //判断异常情况
            if(expressDetail == null || expressDetail.getTrackList() == null){
                mStateLayout.showEmptyView(true);
                return;
            }
            Bundle bundle = new Bundle();
            try{
                //快递名称
                bundle.putString("expressName", expressDetail.getExpressName());
                //运单号
                bundle.putString("expressNo", expressDetail.getExpressNo());
                //运单状态码
                bundle.putInt("trackStatusCode", expressDetail.getTrackStatusCode());
                //收货地址
                bundle.putString("receiveAddress", expressDetail.getReceiveAddress());
                //发货地址
                bundle.putString("sendAddress", expressDetail.getSendAddress());
                //数据源
                bundle.putParcelableArrayList("trackList", expressDetail.getTrackList());
                //parseJson And setView
                setData(expressDetail.getTrackList().size(), 4, ExpressDetailItemFragment.class.getName()
                        , bundle, expressDetail.getTrackList());
            }catch (Exception e){
                e.printStackTrace();
                ToastUtils.showToast("数据源为空");
            }
        } else {
            //default

        }
    }

    /**
     * requestError
     * @param errorInfo
     */
    @Override
    public void onLoadError(String errorInfo) {
        //异常信息是否收集 TODO
        System.out.println(errorInfo);
    }

    /**
     * navigation 设置标题
     */
    @Override
    public void setTitle() {
        if (peekFragment() instanceof ExpressDetailFragment) {
            mActivity.setTitle(getString(R.string.query_express_detail));
        }
    }

    /**
     * 屏蔽下拉
     */
    @Override
    public void onDownPull() {
    }

    /**
     * stateLayout click to refresh
     */
    @Override
    public void onRefreshListener() {
        Map<String, Object> map = new HashMap<>();
        map.put("packageUkid", packageUkid);
        loadData(ContractConstant.QUERY_EXPRESS_DETAIL, map, QUERY_EXPRESS_DETAIL_CODE);
    }

    /**
     * destory
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
